#define DEBUG
#include <cstdio>

using namespace std;

const int maxn=100000;

void solve(int o, int *fa, int *val, int *ans) {
  static int book[maxn+1];
  book[0]=1, book[1]=val[o], ans[o]=1;
  for (int i=fa[o]; i>0; i=fa[i]) {
    if (book[book[0]]<=val[i]) {
      book[++book[0]]=val[i];
    } else {
      int pos=0;
      for (int l=1, r=book[0], mid; l<r; ) {
	mid=(l+r)/2;
	if (book[mid]<=val[i]) {
	  pos=mid;
	  l = mid+1;
	} else {
	  r = mid-1;
	}
      }
      book[pos+1]=val[i];
    }
    if (book[0]>ans[i]) {
      ans[i]=book[0];
    }
  }
}

int main() {
#ifdef DEBUG
  freopen("1.in", "r", stdin);
  freopen("1.out", "w", stdout);
#endif

  static int fa[maxn+1], val[maxn+1], ans[maxn+1];
  static bool son[maxn+1];
  int n;
  scanf("%d", &n);
  for (int i=1; i<=n; i++) {
    scanf("%d", fa+i);
    if (fa[i]>0) {
      son[fa[i]]=true;
    }
  }
  for (int i=1; i<=n; i++) {
    scanf("%d", &val[i]);
  }

  for (int i=n; i>0; i--) {
    if (!son[i]) {
      solve(i, fa, val, ans);
    }
  }

  for (int i=1; i<=n; i++) {
    printf("%d ", ans[i]);
  }

  fclose(stdin);
  fclose(stdout);
  return 0;
}
